﻿using System;
using System.Drawing;

namespace TodoSoft.Controls
{
	public class TreeGridColumn
	{
		public bool ShrinkToFit;
		public int MinFontSize = 3;
		public readonly TreeGridGroup Group;
		public string Caption { get; set;}
		public Margins Padding { get; set; }
		public StringFormat StringFormat = new StringFormat();
		public int Width { get; set;}
		public int Left = 0;
        public int Right { get { return Left + Width;}}

		public event Action<TreeGridCell> CellClick;

		private string _format = string.Empty;
		private string _formatArgument = "{0}";

		public string FormatString
		{
			get
			{
				return _format;
			}
			set
			{
				_format = value;
				_formatArgument = 
					!string.IsNullOrEmpty(_format) ? 
					"{0:" + _format + "}":
					_formatArgument = "{0}";
			}
		}

		public TreeGridColumn(TreeGridGroup @group, string caption, int width)
		{
			Group = @group;
			Caption = caption;
			Width = width;
		}

		internal void Paint(Graphics graphics)
		{
			graphics.DrawString( Caption,
							Group.HeaderRow.Font,
							Group.HeaderRow.TextBrush,
							new Rectangle(
								Left,
								Group.HeaderRow.Top,
								Width,
								Group.HeaderRow.Height)
							);
		}

		internal string Format(object data)
		{
			return string.Format(_formatArgument, data);
		}

		public void OnCellClick(TreeGridCell cell)
		{
			if(CellClick != null)
			 CellClick(cell);
		}
	}
}
